{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "loose-volunteer",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_csv('pokemon_data.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cellular-daughter",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.iloc[1:5, 2:8]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "racial-requirement",
   "metadata": {},
   "outputs": [],
   "source": [
    "for index, r in df.iterrows():\n",
    "    print(index, r.体力, r.攻击)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "drawn-tiffany",
   "metadata": {},
   "outputs": [],
   "source": [
    "df[df['Type 1'].str.contains('Fire|Rock', regex=True)]\n",
    "import re\n",
    "df[df['Type 1'].str.contains('fire|Rock', flags=re.I, regex=True)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "duplicate-headquarters",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('pokemon_data.csv')\n",
    "\n",
    "# 使用loc可以做同样的过滤，并且可以修改\n",
    "df.loc[df['名字'].str.contains('saur')]\n",
    "\n",
    "# 用loc同时修改，注意逗号用法，讲解看视频\n",
    "df.loc[df['名字'].str.contains('saur'), 'Type 2'] = '麦叔'\n",
    "\n",
    "# 同时修改两列\n",
    "df.loc[df['名字'].str.contains('saur'), ['Type 1','Type 2']] = '麦叔'\n",
    "\n",
    "# # 用不同的值修改两列\n",
    "df.loc[df['名字'].str.contains('saur'), ['Type 1','Type 2']] = ['麦叔1','麦叔2']\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "structural-necessity",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>学号</th>\n",
       "      <th>姓名</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>Python</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>9529</td>\n",
       "      <td>王五</td>\n",
       "      <td>98</td>\n",
       "      <td>89</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>9527</td>\n",
       "      <td>张三</td>\n",
       "      <td>98</td>\n",
       "      <td>78</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>9528</td>\n",
       "      <td>李四</td>\n",
       "      <td>87</td>\n",
       "      <td>96</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>9530</td>\n",
       "      <td>麦叔</td>\n",
       "      <td>68</td>\n",
       "      <td>99</td>\n",
       "      <td>97</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     学号  姓名  语文  数学  Python\n",
       "2  9529  王五  98  89      99\n",
       "0  9527  张三  98  78      88\n",
       "1  9528  李四  87  96     100\n",
       "3  9530  麦叔  68  99      97"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = {'学号':[9527, 9528, 9529, 9530],\n",
    "        '姓名':['张三', '李四', '王五', '麦叔'],\n",
    "        '语文':[98, 87, 98, 68],\n",
    "        '数学':[78, 96, 89, 99],\n",
    "        'Python':[88, 100, 99, 97]\n",
    "       }\n",
    "df = pd.DataFrame(data)\n",
    "\n",
    "# 根据索引排序\n",
    "# df.sort_index()\n",
    "\n",
    "# # 指定倒着排序\n",
    "#df.sort_index(inplace=True, ascending=False)\n",
    "\n",
    "# # 根据某一列排序\n",
    "df.sort_values('语文', ascending=True)\n",
    "\n",
    "# # 根据某多列排序\n",
    "df.sort_values(['语文', '数学'], ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "breathing-spain",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     学号  姓名  语文  数学  Python\n",
      "0  9527  张三  98  78      88\n",
      "1  9528  李四  87  96     100\n",
      "2  9529  王五  98  89      99\n",
      "3  9530  麦叔  68  99      97\n",
      "0    False\n",
      "1     True\n",
      "2    False\n",
      "3    False\n",
      "Name: 数学, dtype: bool\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>学号</th>\n",
       "      <th>姓名</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>Python</th>\n",
       "      <th>语文排名</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>9528</td>\n",
       "      <td>李四</td>\n",
       "      <td>87</td>\n",
       "      <td>96</td>\n",
       "      <td>100</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     学号  姓名  语文  数学  Python  语文排名\n",
       "1  9528  李四  87  96     100   3.0"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = {'学号':[9527, 9528, 9529, 9530],\n",
    "        '姓名':['张三', '李四', '王五', '麦叔'],\n",
    "        '语文':[98, 87, 98, 68],\n",
    "        '数学':[78, 96, 89, 99],\n",
    "        'Python':[88, 100, 99, 97]\n",
    "       }\n",
    "df = pd.DataFrame(data)\n",
    "print(df)\n",
    "df.语文.rank(method='dense')\n",
    "df.rank(numeric_only = True) \n",
    "df['语文排名'] = df.语文.rank(ascending=False, method='min')\n",
    "print(df.数学.rank(method='dense', ascending=False) == 2.0)\n",
    "df[df.数学.rank(method='dense', ascending=False) == 2.0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "twenty-algebra",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     学号  姓名  语文  数学  Python\n",
      "0  9527  张三  98  78      88\n",
      "1  9528  李四  87  96     100\n",
      "2  9529  王五  98  89      99\n",
      "3  9530  麦叔  68  99      97\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "pandas.core.series.Series"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = {'学号':[9527, 9528, 9529, 9530],\n",
    "        '姓名':['张三', '李四', '王五', '麦叔'],\n",
    "        '语文':[98, 87, 98, 68],\n",
    "        '数学':[78, 96, 89, 99],\n",
    "        'Python':[88, 100, 99, 97]\n",
    "       }\n",
    "df = pd.DataFrame(data)\n",
    "print(df)\n",
    "df.cumsum()\n",
    "type(df.min())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "convertible-conducting",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>学号</th>\n",
       "      <td>9527</td>\n",
       "      <td>9528</td>\n",
       "      <td>9529</td>\n",
       "      <td>9530</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>姓名</th>\n",
       "      <td>张三</td>\n",
       "      <td>李四</td>\n",
       "      <td>王五</td>\n",
       "      <td>麦叔</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>语文</th>\n",
       "      <td>98</td>\n",
       "      <td>87</td>\n",
       "      <td>98</td>\n",
       "      <td>68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>数学</th>\n",
       "      <td>78</td>\n",
       "      <td>96</td>\n",
       "      <td>89</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Python</th>\n",
       "      <td>88</td>\n",
       "      <td>100</td>\n",
       "      <td>99</td>\n",
       "      <td>97</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           0     1     2     3\n",
       "学号      9527  9528  9529  9530\n",
       "姓名        张三    李四    王五    麦叔\n",
       "语文        98    87    98    68\n",
       "数学        78    96    89    99\n",
       "Python    88   100    99    97"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "id": "enclosed-auditor",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>#</th>\n",
       "      <th>名字</th>\n",
       "      <th>Type 1</th>\n",
       "      <th>Type 2</th>\n",
       "      <th>体力</th>\n",
       "      <th>攻击</th>\n",
       "      <th>防守</th>\n",
       "      <th>Sp. Atk</th>\n",
       "      <th>Sp. Def</th>\n",
       "      <th>速度</th>\n",
       "      <th>Generation</th>\n",
       "      <th>Legendary</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Bulbasaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>88</td>\n",
       "      <td>49</td>\n",
       "      <td>49</td>\n",
       "      <td>65</td>\n",
       "      <td>65</td>\n",
       "      <td>45</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Ivysaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>99</td>\n",
       "      <td>62</td>\n",
       "      <td>63</td>\n",
       "      <td>80</td>\n",
       "      <td>80</td>\n",
       "      <td>60</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>Venusaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>77</td>\n",
       "      <td>100</td>\n",
       "      <td>80</td>\n",
       "      <td>100</td>\n",
       "      <td>100</td>\n",
       "      <td>80</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>VenusaurMega Venusaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>66</td>\n",
       "      <td>100</td>\n",
       "      <td>80</td>\n",
       "      <td>122</td>\n",
       "      <td>120</td>\n",
       "      <td>80</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   #                     名字 Type 1  Type 2  体力   攻击  防守  Sp. Atk  Sp. Def  速度  \\\n",
       "0  1              Bulbasaur  Grass  Poison  88   49  49       65       65  45   \n",
       "1  2                Ivysaur  Grass  Poison  99   62  63       80       80  60   \n",
       "2  3               Venusaur  Grass  Poison  77  100  80      100      100  80   \n",
       "3  3  VenusaurMega Venusaur  Grass  Poison  66  100  80      122      120  80   \n",
       "\n",
       "   Generation  Legendary  \n",
       "0           1      False  \n",
       "1           1      False  \n",
       "2           1      False  \n",
       "3           1      False  "
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_csv('pokemon_data.csv')\n",
    "\n",
    "# 修改数据就是使用赋值，我们可以用多种方法修改数据\n",
    "\n",
    "# 修改单个数据\n",
    "#df.iloc[3, 1] = '麦叔'\n",
    "\n",
    "# # 修改选中的数据：把体力大于60的记录的攻击改成100\n",
    "df.loc[df.体力>60, '攻击'] = 100\n",
    "\n",
    "# # 修改多列\n",
    "df.loc[df.体力>60, ['攻击','防守']] = 100\n",
    "\n",
    "# # 给不同的列赋予不同的值\n",
    "df.loc[df.体力>60,['攻击','防守']] = [100, 80]\n",
    "\n",
    "# #给每个格子赋予不同的值\n",
    "df.loc[df.名字.str.contains('saur'), '体力'] = [88, 99, 77, 66] \n",
    "df.loc[df.名字.str.contains('saur')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "id": "seasonal-vatican",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>#</th>\n",
       "      <th>名字</th>\n",
       "      <th>Type 1</th>\n",
       "      <th>Type 2</th>\n",
       "      <th>体力</th>\n",
       "      <th>攻击</th>\n",
       "      <th>防守</th>\n",
       "      <th>Sp. Atk</th>\n",
       "      <th>Sp. Def</th>\n",
       "      <th>速度</th>\n",
       "      <th>Generation</th>\n",
       "      <th>Legendary</th>\n",
       "      <th>total</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Bulbasaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>88</td>\n",
       "      <td>49</td>\n",
       "      <td>49</td>\n",
       "      <td>65</td>\n",
       "      <td>65</td>\n",
       "      <td>45</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "      <td>361</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Ivysaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>99</td>\n",
       "      <td>62</td>\n",
       "      <td>63</td>\n",
       "      <td>80</td>\n",
       "      <td>80</td>\n",
       "      <td>60</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "      <td>444</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>Venusaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>77</td>\n",
       "      <td>100</td>\n",
       "      <td>80</td>\n",
       "      <td>100</td>\n",
       "      <td>100</td>\n",
       "      <td>80</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "      <td>537</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>VenusaurMega Venusaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>66</td>\n",
       "      <td>100</td>\n",
       "      <td>80</td>\n",
       "      <td>122</td>\n",
       "      <td>120</td>\n",
       "      <td>80</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "      <td>568</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>Charmander</td>\n",
       "      <td>Fire</td>\n",
       "      <td>NaN</td>\n",
       "      <td>39</td>\n",
       "      <td>52</td>\n",
       "      <td>43</td>\n",
       "      <td>60</td>\n",
       "      <td>50</td>\n",
       "      <td>65</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "      <td>309</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>795</th>\n",
       "      <td>719</td>\n",
       "      <td>Diancie</td>\n",
       "      <td>Rock</td>\n",
       "      <td>Fairy</td>\n",
       "      <td>50</td>\n",
       "      <td>100</td>\n",
       "      <td>150</td>\n",
       "      <td>100</td>\n",
       "      <td>150</td>\n",
       "      <td>50</td>\n",
       "      <td>6</td>\n",
       "      <td>True</td>\n",
       "      <td>600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>796</th>\n",
       "      <td>719</td>\n",
       "      <td>DiancieMega Diancie</td>\n",
       "      <td>Rock</td>\n",
       "      <td>Fairy</td>\n",
       "      <td>50</td>\n",
       "      <td>160</td>\n",
       "      <td>110</td>\n",
       "      <td>160</td>\n",
       "      <td>110</td>\n",
       "      <td>110</td>\n",
       "      <td>6</td>\n",
       "      <td>True</td>\n",
       "      <td>700</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>797</th>\n",
       "      <td>720</td>\n",
       "      <td>HoopaHoopa Confined</td>\n",
       "      <td>Psychic</td>\n",
       "      <td>Ghost</td>\n",
       "      <td>80</td>\n",
       "      <td>100</td>\n",
       "      <td>80</td>\n",
       "      <td>150</td>\n",
       "      <td>130</td>\n",
       "      <td>70</td>\n",
       "      <td>6</td>\n",
       "      <td>True</td>\n",
       "      <td>610</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>798</th>\n",
       "      <td>720</td>\n",
       "      <td>HoopaHoopa Unbound</td>\n",
       "      <td>Psychic</td>\n",
       "      <td>Dark</td>\n",
       "      <td>80</td>\n",
       "      <td>100</td>\n",
       "      <td>80</td>\n",
       "      <td>170</td>\n",
       "      <td>130</td>\n",
       "      <td>80</td>\n",
       "      <td>6</td>\n",
       "      <td>True</td>\n",
       "      <td>640</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>799</th>\n",
       "      <td>721</td>\n",
       "      <td>Volcanion</td>\n",
       "      <td>Fire</td>\n",
       "      <td>Water</td>\n",
       "      <td>80</td>\n",
       "      <td>100</td>\n",
       "      <td>80</td>\n",
       "      <td>130</td>\n",
       "      <td>90</td>\n",
       "      <td>70</td>\n",
       "      <td>6</td>\n",
       "      <td>True</td>\n",
       "      <td>550</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>800 rows × 13 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       #                     名字   Type 1  Type 2  体力   攻击   防守  Sp. Atk  \\\n",
       "0      1              Bulbasaur    Grass  Poison  88   49   49       65   \n",
       "1      2                Ivysaur    Grass  Poison  99   62   63       80   \n",
       "2      3               Venusaur    Grass  Poison  77  100   80      100   \n",
       "3      3  VenusaurMega Venusaur    Grass  Poison  66  100   80      122   \n",
       "4      4             Charmander     Fire     NaN  39   52   43       60   \n",
       "..   ...                    ...      ...     ...  ..  ...  ...      ...   \n",
       "795  719                Diancie     Rock   Fairy  50  100  150      100   \n",
       "796  719    DiancieMega Diancie     Rock   Fairy  50  160  110      160   \n",
       "797  720    HoopaHoopa Confined  Psychic   Ghost  80  100   80      150   \n",
       "798  720     HoopaHoopa Unbound  Psychic    Dark  80  100   80      170   \n",
       "799  721              Volcanion     Fire   Water  80  100   80      130   \n",
       "\n",
       "     Sp. Def   速度  Generation  Legendary  total  \n",
       "0         65   45           1      False    361  \n",
       "1         80   60           1      False    444  \n",
       "2        100   80           1      False    537  \n",
       "3        120   80           1      False    568  \n",
       "4         50   65           1      False    309  \n",
       "..       ...  ...         ...        ...    ...  \n",
       "795      150   50           6       True    600  \n",
       "796      110  110           6       True    700  \n",
       "797      130   70           6       True    610  \n",
       "798      130   80           6       True    640  \n",
       "799       90   70           6       True    550  \n",
       "\n",
       "[800 rows x 13 columns]"
      ]
     },
     "execution_count": 150,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['total'] = 100\n",
    "df['total'] = df['体力'] + df['攻击']\n",
    "# 另一种计算方法\n",
    "df['total'] = df.loc[:, '体力':'速度'].sum(axis=1)\n",
    "\n",
    "# 也可以这样写\n",
    "df['total'] = df.iloc[:, 4:10].sum(axis=1)\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "id": "widespread-stable",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>#</th>\n",
       "      <th>名字</th>\n",
       "      <th>Type 1</th>\n",
       "      <th>Type 2</th>\n",
       "      <th>体力</th>\n",
       "      <th>攻击</th>\n",
       "      <th>防守</th>\n",
       "      <th>Sp. Atk</th>\n",
       "      <th>Sp. Def</th>\n",
       "      <th>速度</th>\n",
       "      <th>Generation</th>\n",
       "      <th>Legendary</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>Charmander</td>\n",
       "      <td>Fire</td>\n",
       "      <td>NaN</td>\n",
       "      <td>39</td>\n",
       "      <td>52</td>\n",
       "      <td>43</td>\n",
       "      <td>60</td>\n",
       "      <td>50</td>\n",
       "      <td>65</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5</td>\n",
       "      <td>Charmeleon</td>\n",
       "      <td>Fire</td>\n",
       "      <td>NaN</td>\n",
       "      <td>58</td>\n",
       "      <td>64</td>\n",
       "      <td>58</td>\n",
       "      <td>80</td>\n",
       "      <td>65</td>\n",
       "      <td>80</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>6</td>\n",
       "      <td>Charizard</td>\n",
       "      <td>Fire</td>\n",
       "      <td>Flying</td>\n",
       "      <td>78</td>\n",
       "      <td>84</td>\n",
       "      <td>78</td>\n",
       "      <td>109</td>\n",
       "      <td>85</td>\n",
       "      <td>100</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>6</td>\n",
       "      <td>CharizardMega Charizard X</td>\n",
       "      <td>Fire</td>\n",
       "      <td>Dragon</td>\n",
       "      <td>78</td>\n",
       "      <td>130</td>\n",
       "      <td>111</td>\n",
       "      <td>130</td>\n",
       "      <td>85</td>\n",
       "      <td>100</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>6</td>\n",
       "      <td>CharizardMega Charizard Y</td>\n",
       "      <td>Fire</td>\n",
       "      <td>Flying</td>\n",
       "      <td>78</td>\n",
       "      <td>104</td>\n",
       "      <td>78</td>\n",
       "      <td>159</td>\n",
       "      <td>115</td>\n",
       "      <td>100</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>795</th>\n",
       "      <td>719</td>\n",
       "      <td>Diancie</td>\n",
       "      <td>Rock</td>\n",
       "      <td>Fairy</td>\n",
       "      <td>50</td>\n",
       "      <td>100</td>\n",
       "      <td>150</td>\n",
       "      <td>100</td>\n",
       "      <td>150</td>\n",
       "      <td>50</td>\n",
       "      <td>6</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>796</th>\n",
       "      <td>719</td>\n",
       "      <td>DiancieMega Diancie</td>\n",
       "      <td>Rock</td>\n",
       "      <td>Fairy</td>\n",
       "      <td>50</td>\n",
       "      <td>160</td>\n",
       "      <td>110</td>\n",
       "      <td>160</td>\n",
       "      <td>110</td>\n",
       "      <td>110</td>\n",
       "      <td>6</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>797</th>\n",
       "      <td>720</td>\n",
       "      <td>HoopaHoopa Confined</td>\n",
       "      <td>Psychic</td>\n",
       "      <td>Ghost</td>\n",
       "      <td>80</td>\n",
       "      <td>110</td>\n",
       "      <td>60</td>\n",
       "      <td>150</td>\n",
       "      <td>130</td>\n",
       "      <td>70</td>\n",
       "      <td>6</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>798</th>\n",
       "      <td>720</td>\n",
       "      <td>HoopaHoopa Unbound</td>\n",
       "      <td>Psychic</td>\n",
       "      <td>Dark</td>\n",
       "      <td>80</td>\n",
       "      <td>160</td>\n",
       "      <td>60</td>\n",
       "      <td>170</td>\n",
       "      <td>130</td>\n",
       "      <td>80</td>\n",
       "      <td>6</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>799</th>\n",
       "      <td>721</td>\n",
       "      <td>Volcanion</td>\n",
       "      <td>Fire</td>\n",
       "      <td>Water</td>\n",
       "      <td>80</td>\n",
       "      <td>110</td>\n",
       "      <td>120</td>\n",
       "      <td>130</td>\n",
       "      <td>90</td>\n",
       "      <td>70</td>\n",
       "      <td>6</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>730 rows × 12 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       #                         名字   Type 1  Type 2  体力   攻击   防守  Sp. Atk  \\\n",
       "4      4                 Charmander     Fire     NaN  39   52   43       60   \n",
       "5      5                 Charmeleon     Fire     NaN  58   64   58       80   \n",
       "6      6                  Charizard     Fire  Flying  78   84   78      109   \n",
       "7      6  CharizardMega Charizard X     Fire  Dragon  78  130  111      130   \n",
       "8      6  CharizardMega Charizard Y     Fire  Flying  78  104   78      159   \n",
       "..   ...                        ...      ...     ...  ..  ...  ...      ...   \n",
       "795  719                    Diancie     Rock   Fairy  50  100  150      100   \n",
       "796  719        DiancieMega Diancie     Rock   Fairy  50  160  110      160   \n",
       "797  720        HoopaHoopa Confined  Psychic   Ghost  80  110   60      150   \n",
       "798  720         HoopaHoopa Unbound  Psychic    Dark  80  160   60      170   \n",
       "799  721                  Volcanion     Fire   Water  80  110  120      130   \n",
       "\n",
       "     Sp. Def   速度  Generation  Legendary  \n",
       "4         50   65           1      False  \n",
       "5         65   80           1      False  \n",
       "6         85  100           1      False  \n",
       "7         85  100           1      False  \n",
       "8        115  100           1      False  \n",
       "..       ...  ...         ...        ...  \n",
       "795      150   50           6       True  \n",
       "796      110  110           6       True  \n",
       "797      130   70           6       True  \n",
       "798      130   80           6       True  \n",
       "799       90   70           6       True  \n",
       "\n",
       "[730 rows x 12 columns]"
      ]
     },
     "execution_count": 162,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除行\n",
    "# df1 = df.drop(0)\n",
    "\n",
    "# 原地删除\n",
    "#df.drop(0, inplace=True)\n",
    "\n",
    "\n",
    "# 删除列，默认是删除行，如果要删除列，需要加上axis=1\n",
    "df = pd.read_csv('pokemon_data.csv')\n",
    "#df.drop(['名字', 'Type 1'], axis=1, inplace=True)\n",
    "\n",
    "\n",
    "# # 删除多行和列\n",
    "#df.drop([1,3,5,7], inplace=True)\n",
    "\n",
    "\n",
    "# # 根据条件删除\n",
    "index = df.index[df['Type 1'] == 'Grass']\n",
    "df1 = df.drop(index)\n",
    "\n",
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "id": "metropolitan-chuck",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>#</th>\n",
       "      <th>名字</th>\n",
       "      <th>Type 1</th>\n",
       "      <th>Type 2</th>\n",
       "      <th>体力</th>\n",
       "      <th>攻击</th>\n",
       "      <th>防守</th>\n",
       "      <th>Sp. Atk</th>\n",
       "      <th>Sp. Def</th>\n",
       "      <th>速度</th>\n",
       "      <th>Generation</th>\n",
       "      <th>Legendary</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Bulbasaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>45</td>\n",
       "      <td>49</td>\n",
       "      <td>49</td>\n",
       "      <td>65</td>\n",
       "      <td>65</td>\n",
       "      <td>45</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Ivysaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>60</td>\n",
       "      <td>62</td>\n",
       "      <td>63</td>\n",
       "      <td>80</td>\n",
       "      <td>80</td>\n",
       "      <td>60</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>Venusaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>80</td>\n",
       "      <td>82</td>\n",
       "      <td>83</td>\n",
       "      <td>100</td>\n",
       "      <td>100</td>\n",
       "      <td>80</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>VenusaurMega Venusaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>80</td>\n",
       "      <td>100</td>\n",
       "      <td>123</td>\n",
       "      <td>122</td>\n",
       "      <td>120</td>\n",
       "      <td>80</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>Charmander</td>\n",
       "      <td>Fire</td>\n",
       "      <td>NaN</td>\n",
       "      <td>39</td>\n",
       "      <td>52</td>\n",
       "      <td>43</td>\n",
       "      <td>60</td>\n",
       "      <td>50</td>\n",
       "      <td>65</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>795</th>\n",
       "      <td>719</td>\n",
       "      <td>Diancie</td>\n",
       "      <td>Rock</td>\n",
       "      <td>Fairy</td>\n",
       "      <td>50</td>\n",
       "      <td>100</td>\n",
       "      <td>150</td>\n",
       "      <td>100</td>\n",
       "      <td>150</td>\n",
       "      <td>50</td>\n",
       "      <td>6</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>796</th>\n",
       "      <td>719</td>\n",
       "      <td>DiancieMega Diancie</td>\n",
       "      <td>Rock</td>\n",
       "      <td>Fairy</td>\n",
       "      <td>50</td>\n",
       "      <td>160</td>\n",
       "      <td>110</td>\n",
       "      <td>160</td>\n",
       "      <td>110</td>\n",
       "      <td>110</td>\n",
       "      <td>6</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>797</th>\n",
       "      <td>720</td>\n",
       "      <td>HoopaHoopa Confined</td>\n",
       "      <td>Psychic</td>\n",
       "      <td>Ghost</td>\n",
       "      <td>80</td>\n",
       "      <td>110</td>\n",
       "      <td>60</td>\n",
       "      <td>150</td>\n",
       "      <td>130</td>\n",
       "      <td>70</td>\n",
       "      <td>6</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>798</th>\n",
       "      <td>720</td>\n",
       "      <td>HoopaHoopa Unbound</td>\n",
       "      <td>Psychic</td>\n",
       "      <td>Dark</td>\n",
       "      <td>80</td>\n",
       "      <td>160</td>\n",
       "      <td>60</td>\n",
       "      <td>170</td>\n",
       "      <td>130</td>\n",
       "      <td>80</td>\n",
       "      <td>6</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>799</th>\n",
       "      <td>721</td>\n",
       "      <td>Volcanion</td>\n",
       "      <td>Fire</td>\n",
       "      <td>Water</td>\n",
       "      <td>80</td>\n",
       "      <td>110</td>\n",
       "      <td>120</td>\n",
       "      <td>130</td>\n",
       "      <td>90</td>\n",
       "      <td>70</td>\n",
       "      <td>6</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>800 rows × 12 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       #                     名字   Type 1  Type 2  体力   攻击   防守  Sp. Atk  \\\n",
       "0      1              Bulbasaur    Grass  Poison  45   49   49       65   \n",
       "1      2                Ivysaur    Grass  Poison  60   62   63       80   \n",
       "2      3               Venusaur    Grass  Poison  80   82   83      100   \n",
       "3      3  VenusaurMega Venusaur    Grass  Poison  80  100  123      122   \n",
       "4      4             Charmander     Fire     NaN  39   52   43       60   \n",
       "..   ...                    ...      ...     ...  ..  ...  ...      ...   \n",
       "795  719                Diancie     Rock   Fairy  50  100  150      100   \n",
       "796  719    DiancieMega Diancie     Rock   Fairy  50  160  110      160   \n",
       "797  720    HoopaHoopa Confined  Psychic   Ghost  80  110   60      150   \n",
       "798  720     HoopaHoopa Unbound  Psychic    Dark  80  160   60      170   \n",
       "799  721              Volcanion     Fire   Water  80  110  120      130   \n",
       "\n",
       "     Sp. Def   速度  Generation  Legendary  \n",
       "0         65   45           1      False  \n",
       "1         80   60           1      False  \n",
       "2        100   80           1      False  \n",
       "3        120   80           1      False  \n",
       "4         50   65           1      False  \n",
       "..       ...  ...         ...        ...  \n",
       "795      150   50           6       True  \n",
       "796      110  110           6       True  \n",
       "797      130   70           6       True  \n",
       "798      130   80           6       True  \n",
       "799       90   70           6       True  \n",
       "\n",
       "[800 rows x 12 columns]"
      ]
     },
     "execution_count": 163,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_csv('pokemon_data.csv')\n",
    "df['total'] = 100\n",
    "\n",
    "# 创建表示新的列名顺序的列表\n",
    "cols = ['total'] + df.columns.to_list()[0:-1]\n",
    "df = df[cols]\n",
    "\n",
    "#练习：把total加在体力前，先删掉原来的，多一次练习，可以不删除\n",
    "df.drop('total', axis=1, inplace=True)\n",
    "\n",
    "# 重新添加\n",
    "df['total'] = df.iloc[:, 4:10].sum(axis=1)\n",
    "cols = df.columns.to_list()[0:4] + ['total'] + df.columns.to_list()[4:-1]\n",
    "df = df[cols]\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "id": "bulgarian-timeline",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>#</th>\n",
       "      <th>名字</th>\n",
       "      <th>Type 1</th>\n",
       "      <th>Type 2</th>\n",
       "      <th>total</th>\n",
       "      <th>体力</th>\n",
       "      <th>攻击</th>\n",
       "      <th>防守</th>\n",
       "      <th>Sp. Atk</th>\n",
       "      <th>Sp. Def</th>\n",
       "      <th>速度</th>\n",
       "      <th>Generation</th>\n",
       "      <th>Legendary</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Bulbasaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>318</td>\n",
       "      <td>45</td>\n",
       "      <td>49</td>\n",
       "      <td>49</td>\n",
       "      <td>65</td>\n",
       "      <td>65</td>\n",
       "      <td>45</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Ivysaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>405</td>\n",
       "      <td>60</td>\n",
       "      <td>62</td>\n",
       "      <td>63</td>\n",
       "      <td>80</td>\n",
       "      <td>80</td>\n",
       "      <td>60</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>Venusaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>525</td>\n",
       "      <td>80</td>\n",
       "      <td>82</td>\n",
       "      <td>83</td>\n",
       "      <td>100</td>\n",
       "      <td>100</td>\n",
       "      <td>80</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>VenusaurMega Venusaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>625</td>\n",
       "      <td>80</td>\n",
       "      <td>100</td>\n",
       "      <td>123</td>\n",
       "      <td>122</td>\n",
       "      <td>120</td>\n",
       "      <td>80</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>Charmander</td>\n",
       "      <td>Fire</td>\n",
       "      <td>NaN</td>\n",
       "      <td>309</td>\n",
       "      <td>39</td>\n",
       "      <td>52</td>\n",
       "      <td>43</td>\n",
       "      <td>60</td>\n",
       "      <td>50</td>\n",
       "      <td>65</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>795</th>\n",
       "      <td>719</td>\n",
       "      <td>Diancie</td>\n",
       "      <td>Rock</td>\n",
       "      <td>Fairy</td>\n",
       "      <td>600</td>\n",
       "      <td>50</td>\n",
       "      <td>100</td>\n",
       "      <td>150</td>\n",
       "      <td>100</td>\n",
       "      <td>150</td>\n",
       "      <td>50</td>\n",
       "      <td>6</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>796</th>\n",
       "      <td>719</td>\n",
       "      <td>DiancieMega Diancie</td>\n",
       "      <td>Rock</td>\n",
       "      <td>Fairy</td>\n",
       "      <td>700</td>\n",
       "      <td>50</td>\n",
       "      <td>160</td>\n",
       "      <td>110</td>\n",
       "      <td>160</td>\n",
       "      <td>110</td>\n",
       "      <td>110</td>\n",
       "      <td>6</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>797</th>\n",
       "      <td>720</td>\n",
       "      <td>HoopaHoopa Confined</td>\n",
       "      <td>Psychic</td>\n",
       "      <td>Ghost</td>\n",
       "      <td>600</td>\n",
       "      <td>80</td>\n",
       "      <td>110</td>\n",
       "      <td>60</td>\n",
       "      <td>150</td>\n",
       "      <td>130</td>\n",
       "      <td>70</td>\n",
       "      <td>6</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>798</th>\n",
       "      <td>720</td>\n",
       "      <td>HoopaHoopa Unbound</td>\n",
       "      <td>Psychic</td>\n",
       "      <td>Dark</td>\n",
       "      <td>680</td>\n",
       "      <td>80</td>\n",
       "      <td>160</td>\n",
       "      <td>60</td>\n",
       "      <td>170</td>\n",
       "      <td>130</td>\n",
       "      <td>80</td>\n",
       "      <td>6</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>799</th>\n",
       "      <td>721</td>\n",
       "      <td>Volcanion</td>\n",
       "      <td>Fire</td>\n",
       "      <td>Water</td>\n",
       "      <td>600</td>\n",
       "      <td>80</td>\n",
       "      <td>110</td>\n",
       "      <td>120</td>\n",
       "      <td>130</td>\n",
       "      <td>90</td>\n",
       "      <td>70</td>\n",
       "      <td>6</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>800 rows × 13 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       #                     名字   Type 1  Type 2  total  体力   攻击   防守  \\\n",
       "0      1              Bulbasaur    Grass  Poison    318  45   49   49   \n",
       "1      2                Ivysaur    Grass  Poison    405  60   62   63   \n",
       "2      3               Venusaur    Grass  Poison    525  80   82   83   \n",
       "3      3  VenusaurMega Venusaur    Grass  Poison    625  80  100  123   \n",
       "4      4             Charmander     Fire     NaN    309  39   52   43   \n",
       "..   ...                    ...      ...     ...    ...  ..  ...  ...   \n",
       "795  719                Diancie     Rock   Fairy    600  50  100  150   \n",
       "796  719    DiancieMega Diancie     Rock   Fairy    700  50  160  110   \n",
       "797  720    HoopaHoopa Confined  Psychic   Ghost    600  80  110   60   \n",
       "798  720     HoopaHoopa Unbound  Psychic    Dark    680  80  160   60   \n",
       "799  721              Volcanion     Fire   Water    600  80  110  120   \n",
       "\n",
       "     Sp. Atk  Sp. Def   速度  Generation  Legendary  \n",
       "0         65       65   45           1      False  \n",
       "1         80       80   60           1      False  \n",
       "2        100      100   80           1      False  \n",
       "3        122      120   80           1      False  \n",
       "4         60       50   65           1      False  \n",
       "..       ...      ...  ...         ...        ...  \n",
       "795      100      150   50           6       True  \n",
       "796      160      110  110           6       True  \n",
       "797      150      130   70           6       True  \n",
       "798      170      130   80           6       True  \n",
       "799      130       90   70           6       True  \n",
       "\n",
       "[800 rows x 13 columns]"
      ]
     },
     "execution_count": 173,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_csv('pokemon_data.csv')\n",
    "df['total'] = 100\n",
    "\n",
    "# 创建表示新的列名顺序的列表\n",
    "cols = ['total'] + df.columns.to_list()[0:-1]\n",
    "df = df[cols]\n",
    "\n",
    "#练习：把total加在体力前，先删掉原来的，多一次练习，可以不删除\n",
    "df.drop('total', axis=1, inplace=True)\n",
    "\n",
    "# # 重新添加\n",
    "df['total'] = df.iloc[:, 4:10].sum(axis=1)\n",
    "cols = df.columns.to_list()[0:4] + ['total'] + df.columns.to_list()[4:-1]\n",
    "df = df[cols]\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "id": "chubby-cache",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_csv('pokemon_data.csv')\n",
    "df['total'] = df.iloc[:, 4:10].sum(axis=1)\n",
    "cols = df.columns.to_list()[0:4] + ['total'] + df.columns.to_list()[4:-1]\n",
    "df = df[cols]\n",
    "\n",
    "df.to_csv('new_file1.csv')\n",
    "df.to_excel('new_file2.xlsx', engine='openpyxl')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "opposite-paintball",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
